//
//  EmoticonPackage.swift
//  14-表情键盘
//
//  Created by 高伦展 on 2018/2/19.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {

    /// 表情包路径名
    var id: String?
    /// 表情包名称
    var group_name_cn: String?
    /// 表情包数组
    lazy var emoticons = [Emoticon]()
    
    init(dict:[String:AnyObject]) {
         super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        if let array = dict["emoticons"] as? [[String:String]]{
            
            var index = 0
            
            for var d in array{
                
                if let png = d["png"],let dir = id{
                    d["png"] = dir + "/" + png
                }
                
                emoticons.append(Emoticon(dict:d as [String : AnyObject]))
            
                index+=1
                if index == 20{
                    emoticons.append(Emoticon(isRemoved: true))
                    
                    index = 0
                }
            }
        }
        
        appendEmptyEmoticon()
    }
    
    ///在表情数组末尾，添加空白表情
    private func appendEmptyEmoticon(){
        let count = emoticons.count % 21
        
        if emoticons.count > 0 && count == 0{
            return
        }
        
        for _ in count..<20{
            emoticons.append(Emoticon(isEmpty: true))
        }
        
        emoticons.append(Emoticon(isRemoved: true))
        
    }
    
    override var description: String{
        let keys = ["id", "group_name_cn", "emoticons"]
        
        return dictionaryWithValues(forKeys: keys).description
    }
}
